package com.lancoo.cvas.schedues;

import com.lancoo.cvas.entity.CssTaskInfo;
import com.lancoo.cvas.entity.CssTaskRound;
import com.lancoo.cvas.service.CssTaskInfoService;
import com.lancoo.cvas.service.CssTaskRoundService;
import com.lancoo.cvas.service.YunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;


@Component
@Slf4j
@EnableAsync(proxyTargetClass = true)
public class SendMsgSchedule implements Serializable {

    @Resource
    private CssTaskRoundService cssTaskRoundService;

    @Resource
    private CssTaskInfoService cssTaskInfoService;

    @Resource
    private YunService yunBasicService;


    /**
     * 选课结束提醒
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void sendSelectCourseEndMsg() {
        cssTaskRoundService.lambdaQuery()
                .ge(CssTaskRound::getEndTime, LocalDateTime.now().withHour(0).withMinute(0).withSecond(0))
                .le(CssTaskRound::getEndTime, LocalDateTime.now().withHour(23).withMinute(59).withSecond(59)).list()
                .forEach(cssTaskRound -> {
                    CssTaskInfo cssTaskInfo = cssTaskInfoService.getById(cssTaskRound.getCssTaskId());
                    if (cssTaskInfo.getRound() == 1) {
                        yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "正选", cssTaskRound.getId());
                    } else if (cssTaskInfo.getRound() == 2) {
                        yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "补选", cssTaskRound.getId());
                    } else if (cssTaskInfo.getRound() == 3) {
                        yunBasicService.sendSelectCourseEndMsg(cssTaskRound.getEndTime(), cssTaskInfo.getTaskId(), "退选", cssTaskRound.getId());
                    }
                });
    }

}
